(function () {
    var SceneManager = window.SceneManager = function () {

        //给场景管理器添加监听
        // this.bindEvent();
        //设置定时器，进入update时+
        this.fno = 0;
        this.score = 0;
        //1表示欢迎屏幕，2表示教程，3表示游戏内容，3表示GameOver
        this.sceneNum = 1;

        //实例化背景
        this.background = new Background();
        //实例化大地
        this.land = new Land();
        //创建管子数组
        this.pipeArr = [];
        //实例化小鸟
        this.bird = new Bird();

        //获取logo设定位置
        this.title = game.imgList.title;
        this.titley0 = -this.title.height;
        //获取开始按钮设定位置
        this.butplay = game.imgList.butplay;
        this.butplayx0 = game.ctx.canvas.width/2-this.butplay.width/2;
        this.butplayy0 = game.ctx.canvas.height;
        this.bird.birdx = game.ctx.canvas.width/2;
        this.bird.birdy = game.ctx.canvas.height*0.38;
        this.birdflyheight = this.bird.birdy;

        //判断小鸟是否撞柱子
        this.isBirdPipe = false;


    };
    SceneManager.prototype.update = function () {
        this.fno++;
        switch (this.sceneNum){
            case 1:
                this.land.update();
                //logo下降动画
                if(this.titley0>=game.ctx.canvas.height*0.2){
                    this.titley0==game.ctx.canvas.height*0.2;
                }else{
                    this.titley0+=this.fno*0.4;
                }
                if(this.butplayy0<=game.ctx.canvas.height*0.6){
                    this.butplayy0==game.ctx.canvas.height*0.6;
                }else{
                    this.butplayy0-=this.fno*0.6;
                }
                this.bird.ready();

                break;
            case 2:
                this.land.update();
                this.bird.ready();

                break;
            case 3:
                this.background.update();
                this.land.update();
                this.bird.update();
                for(var i=this.pipeArr.length-1;i>=0;i--){
                    this.pipeArr[i].update();
                }

                break;
            case 4:
                if(this.noBirdLand){
                    this.bird.update();
                }else{
                    //屏幕闪烁
                    game.ctx.save();
                    if(this.opecity >0.6){
                        this.isOpacity = !this.isOpacity;
                    }
                    this.isOpacity ? this.opecity-=0.2 : this.opecity+=0.2;
                    game.ctx.fillStyle = "rgba(255,255,255,"+this.opecity+")";
                    game.ctx.fillRect(0,0,game.ctx.canvas.width,game.ctx.canvas.height);
                    game.ctx.restore();

                    if(this.opecity<0){
                        this.enter(5);
                    }
                }

                break;
            case 5:

                if(this.overy0<=game.ctx.canvas.height*0.25-5){
                    this.isOverTop = !this.isOverTop;
                }
                if(!this.isOverTop){
                    this.overy0--;
                }else{
                    if(this.overy0<=game.ctx.canvas.height*0.25+5){
                        this.overy0++;
                    }

                }
                 //-5 + 10

                if(this.butplayy0<=game.ctx.canvas.height*0.6){
                    this.butplayy0==game.ctx.canvas.height*0.6;
                }else{
                    this.butplayy0-=this.fno*0.6;
                }
                break;
        }
    };
    SceneManager.prototype.render = function () {
        switch (this.sceneNum){
            case 1:
                this.background.render();
                this.bird.render();
                this.land.render();
                game.ctx.drawImage(this.title,game.ctx.canvas.width/2-this.title.width/2,this.titley0);
                game.ctx.drawImage(this.butplay,this.butplayx0,this.butplayy0);
                break;
            case 2:
                this.background.render();
                this.bird.render();
                //渲染指引图片
                game.ctx.drawImage(game.imgList.tutorial,game.ctx.canvas.width/2-game.imgList.tutorial.width/2,game.ctx.canvas.height*(1-0.618)+12);
                //渲染准备图片
                game.ctx.drawImage(game.imgList.textready,game.ctx.canvas.width/2-game.imgList.textready.width/2,game.ctx.canvas.height*0.25);


                this.land.render();
                break;
            case 3:
                this.background.render();
                this.bird.render();
                this.land.render();
                //实例化管子
                (this.fno%110 == 0) && (new Pipe());
                //遍历管子数组实现管子的渲染和更新
                for(var i=this.pipeArr.length-1;i>=0;i--){
                    //判断管子是否还在数组中，存在才渲染
                    this.pipeArr[i] && this.pipeArr[i].render();
                }
                //渲染分数
                game.ctx.save();
                game.ctx.translate(game.ctx.canvas.width/2-12,0);
                var interspace = 10;
                var numWidth = 24;
                for(var i=0;i<this.score.toString().length;i++){
                    var nowImg = game.numList[this.score.toString().charAt(i)];
                    //定义空隙常数为 （i-（数组长度-1））/2
                    game.ctx.drawImage(nowImg,(interspace+numWidth)*(i-(this.score.toString().length/2-0.5)),100);
                }
                game.ctx.restore();
                break;
            case 4:
                this.background.render();
                this.bird.render();
                this.land.render();

                ((this.fno-50)%110 == 0) && (new Pipe());
                for(var i=this.pipeArr.length-1;i>=0;i--){
                    this.pipeArr[i] && this.pipeArr[i].render();
                }
                //渲染分数
                game.ctx.save();
                game.ctx.translate(game.ctx.canvas.width/2-12,0);
                var interspace = 10;
                var numWidth = 24;
                for(var i=0;i<this.score.toString().length;i++){
                    var nowImg = game.numList[this.score.toString().charAt(i)];
                    //定义空隙常数为 （i-（数组长度-1））/2
                    game.ctx.drawImage(nowImg,(interspace+numWidth)*(i-(this.score.toString().length/2-0.5)),100);
                }
                game.ctx.restore();
                break;
            case 5:
                this.background.render();
                this.bird.render();
                this.land.render();

                ((this.fno-50)%110 == 0) && (new Pipe());
                for(var i=this.pipeArr.length-1;i>=0;i--){
                    this.pipeArr[i] && this.pipeArr[i].render();
                }
                //渲染分数
                game.ctx.save();
                game.ctx.translate(game.ctx.canvas.width/2-12,0);
                var interspace = 10;
                var numWidth = 24;
                for(var i=0;i<this.score.toString().length;i++){
                    var nowImg = game.numList[this.score.toString().charAt(i)];
                    //定义空隙常数为 （i-（数组长度-1））/2
                    game.ctx.drawImage(nowImg,(interspace+numWidth)*(i-(this.score.toString().length/2-0.5)),100);
                }
                game.ctx.restore();

                //渲染结束图片
                game.ctx.drawImage(game.imgList.textgameover,game.ctx.canvas.width/2-game.imgList.textgameover.width/2,this.overy0);
                game.ctx.drawImage(this.butplay,this.butplayx0,this.butplayy0);
                break;

        }
    };
    //进入场景时触发
    SceneManager.prototype.enter = function (num) {
        switch (num){
            case 1:
                //定时器复原
                this.fno = 0;
                this.sceneNum = 1;
                //title butplay 位置复原
                //获取logo设定位置
                this.title = game.imgList.title;
                this.titley0 = -this.title.height;
                //获取开始按钮设定位置
                this.butplay = game.imgList.butplay;
                this.butplayx0 = game.ctx.canvas.width/2-this.butplay.width/2;
                this.butplayy0 = game.ctx.canvas.height;
                break;
            case 2:
                this.sceneNum = 2;
                this.bird.birdx = game.ctx.canvas.width*(1-0.618)-50;
                this.bird.birdy = game.ctx.canvas.height*(1-0.618)+24;
                this.birdflyheight = this.bird.birdy;
                break;
            case 3:
                this.fno = 0;
                this.sceneNum = 3;
                break;
            case 4:
                this.fno = 0;
                this.sceneNum = 4;
                this.noBirdLand = true;
                this.isOpacity = false;
                this.opecity = 0;
                break;
            case 5:
                this.fno = 0;
                this.sceneNum = 5;
                this.overy0 = game.ctx.canvas.height*0.25;
                this.isOverTop = false;
                break;
        }
    };
    //给场景添加监听判断点击事件
    SceneManager.prototype.bindEvent = function () {
        var that = this;

        //添加点击和移动端触摸事件
        if(game.isMobile){
            game.ctx.canvas.addEventListener("touchstart",function (e) {
                clickHandler(e.touches[0].clientX,e.touches[0].clientY);
            });
        }else{
            game.ctx.canvas.onclick = function(e){
                clickHandler(e.clientX,e.clientY);
            };
        }


        function clickHandler(siteX,siteY) {
            switch (that.sceneNum){
                case 1:
                    console.log(1);
                    if(that.butplayx0<siteX && that.butplayx0+that.butplay.width>siteX && that.butplayy0<siteY && that.butplayy0+that.butplay.height>siteY){
                        that.enter(2);
                    }
                    break;
                case 2:
                    that.enter(3);
                    break;
                case 3:
                    that.bird.fly();
                    break;
                case 5:
                    this.sceneNum = 1;
                    game.start();
                    break;
            }
        };
    };
}());